﻿using Mengsk.ProxyDelivery.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Mengsk.ProxyDelivery.Print.Service
{
    class SellerServiceInfoWithCatch
    {
        private static readonly Dictionary<int, Mengsk.ProxyDelivery.Domain.Seller> sellers = new Dictionary<int, ProxyDelivery.Domain.Seller>();

        private static readonly Dictionary<int, Mengsk.ProxyDelivery.Domain.SellerSenderInfo> sellerSenderInfos = new Dictionary<int, ProxyDelivery.Domain.SellerSenderInfo>();

        private static SellerService sellerService = ServiceContainer.GetService<SellerService>();

        private static SellerSenderInfoService sellerSenderInfoService = ServiceContainer.GetService<SellerSenderInfoService>();

        public static Mengsk.ProxyDelivery.Domain.Seller GetSeller(int id)
        {
            lock (sellers)
            {
                if (sellers.ContainsKey(id))
                {
                    return sellers[id];
                }

                var item = sellerService.GetById(id);
                if (item == null)
                {
                    return null;
                }
                sellers[id] = item;
                return item;
            }
        }

        public static Mengsk.ProxyDelivery.Domain.SellerSenderInfo GetSellerSenderInfo(int id)
        {
            lock (sellerSenderInfos)
            {
                if (sellerSenderInfos.ContainsKey(id))
                {
                    return sellerSenderInfos[id];
                }
                var item = sellerSenderInfoService.GetByField("sellerId",id);
                if (item == null)
                {
                    return null;
                }
                sellerSenderInfos[id] = item;
                return item;
            }
        }
    }
}
